#ifndef UTILS_RANDOM_H
#define UTILS_RANDOM_H

#include <utils/types.h>

#define UTILS_RAND_MAX -1ULL

/*
 * On 64-bit architectures, protect against non-terminated C string overflows
 * by zeroing out the first byte of the canary; this leaves 56 bits of entropy.
 */
#ifdef CONFIG_64BIT
# ifdef ___LITTLE_ENDIAN
#  define UTILS_CANARY_MASK 0xffffffffffffff00UL
# else /* big endian, 64 bits: */
#  define UTILS_CANARY_MASK 0x00ffffffffffffffUL
# endif
#else /* 32 bits: */
# define UTILS_CANARY_MASK 0xffffffffUL
#endif

void utils_srand(uint64_t seed);
uint64_t utils_random(void);
uint64_t utils_random_range(uint64_t min, uint64_t max);

static inline uint64_t utils_random_max(uint64_t max)
{
    return utils_random_range(0, max);
}

static inline uint64_t utils_get_random_canary(void)
{
    uint64_t val = utils_random();

    return val & UTILS_CANARY_MASK;
}

#endif /* !UTILS_RANDOM_H */
